package com.voicechat.firstmet.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.voicechat.firstmet.R;
import com.voicechat.firstmet.base.BaseAdapter;
import com.voicechat.firstmet.bean.FristMetBean;
import com.voicechat.firstmet.client.OssConfig;
import com.voicechat.firstmet.utils.GlideLoadUtils;
import com.voicechat.firstmet.utils.Utils;
import com.voicechat.firstmet.view.CircleImageView;

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.adapter
 * @ClassName: AttachmentPersonAdapter
 * @Description: 附件的人adapter
 * @Author: ljp
 * @CreateDate: 2019/1/19 15:39
 * @Version: 1.0
 */
public class AttachmentPersonAdapter extends BaseAdapter<FristMetBean.PageBean.ListBean> {
    private OnClickItem onClickItem;

    public void setOnClickItem(OnClickItem onClickItem) {
        this.onClickItem = onClickItem;
    }

    /**
     * @param context activity_imessage
     * @param tlist   集合
     * @param mLayout 主布局
     */
    public AttachmentPersonAdapter(Context context, List<FristMetBean.PageBean.ListBean> tlist, int mLayout) {
        super(context, tlist, mLayout);
    }

    @Override
    protected RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        return null;
    }

    @Override
    protected RecyclerView.ViewHolder getItemViewHolder(View view) {
        return new ItemViewHolder(view);
    }

    @Override
    protected RecyclerView.ViewHolder getFooterViewHolder(View view) {
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if(viewHolder instanceof ItemViewHolder){
            ItemViewHolder itemViewHolder = (ItemViewHolder) viewHolder;
            FristMetBean.PageBean.ListBean listBean = tlist.get(position);

            if (!listBean.getAvatarUrl().equals(itemViewHolder.imageIcon.getTag(R.id.image_icon))) {
                // 加载图片
                itemViewHolder.imageIcon.setTag(R.id.image_icon, listBean.getAvatarUrl());
                GlideLoadUtils.getInstance().glideLoad(context,OssConfig.OSS_HTTP+listBean.getAvatarUrl(),
                        (itemViewHolder.imageIcon));
            }
            if (listBean.getGender() == 1) {//判断性别  1男 2女
                itemViewHolder.imageSex.setImageResource(R.mipmap.boy);
                itemViewHolder.linearSexAge.setBackgroundResource(R.mipmap.sex_blue);
            } else if (listBean.getGender() == 2) {
                itemViewHolder.imageSex.setImageResource(R.mipmap.girl);
                itemViewHolder.linearSexAge.setBackgroundResource(R.mipmap.sex_red);
            }
            itemViewHolder.tvNikeName.setText(listBean.getNickName());
            itemViewHolder.tvDistance.setText(listBean.getRangle()/1000+"km");
            itemViewHolder.tvSign.setText(listBean.getAutograph());
            if (!TextUtils.isEmpty(listBean.getBirthday())) {
                String substring = listBean.getBirthday().substring(0, 4);
                String time = Utils.getStringTime(new Date());
                int age = (Integer.parseInt(time)) - (Integer.parseInt(substring));
                itemViewHolder.tvAge.setText(age + "");
            }
            if (listBean.getVType()== 1) {//判断是vip等级 0非会员 1月会员 2年会员
                itemViewHolder.imageVip.setVisibility(View.VISIBLE);
                itemViewHolder.imageVip.setImageResource(R.mipmap.vip_month);
                itemViewHolder.tvNikeName.setTextColor(context.getResources().getColor(R.color.colorFF3B5E));
            } else if (listBean.getVType()==2 ) {
                itemViewHolder.imageVip.setVisibility(View.VISIBLE);
                itemViewHolder.imageVip.setImageResource(R.mipmap.vip_year);
                itemViewHolder.tvNikeName.setTextColor(context.getResources().getColor(R.color.colorFF3B5E));
            } else {
                itemViewHolder.imageVip.setVisibility(View.GONE);
                itemViewHolder.tvNikeName.setTextColor(context.getResources().getColor(R.color.color333333));
            }
            if (listBean.getType() ==2) {//判断是否是主播
                itemViewHolder.imageAnchor.setVisibility(View.VISIBLE);
                GlideLoadUtils.getInstance().glideLoad(context, OssConfig.OSS_HTTP + listBean.getExclusiveUrl(),
                        itemViewHolder.imageAnchor);
            } else {
                itemViewHolder.imageAnchor.setVisibility(View.GONE);
            }
            itemViewHolder.view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickItem.setOnClickItem(position,listBean);
                }
            });


        }
    }

    class ItemViewHolder extends BaseViewHolder{
        @BindView(R.id.image_icon)
        CircleImageView imageIcon;
        @BindView(R.id.image_Anchor)
        ImageView imageAnchor;
        @BindView(R.id.tv_nike_name)
        TextView tvNikeName;
        @BindView(R.id.image_sex)
        ImageView imageSex;
        @BindView(R.id.tv_age)
        TextView tvAge;
        @BindView(R.id.linear_sex_age)
        LinearLayout linearSexAge;
        @BindView(R.id.image_vip)
        ImageView imageVip;
        @BindView(R.id.tv_sign)
        TextView tvSign;
        @BindView(R.id.tv_distance)
        TextView tvDistance;
        @BindView(R.id.linear_distance)
        LinearLayout linearDistance;
        View view;
        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            view = itemView;
        }
    }

    public interface OnClickItem{
        void setOnClickItem(int position,FristMetBean.PageBean.ListBean listBean);
    }
}
